<%--
  Created by IntelliJ IDEA.
  User: This
  Date: 2019/7/12
  Time: 21:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>管理系统</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <%@ include file="base/header.jsp" %>
    <style type="text/css">
        label {
            cursor: default;
        }

        input[type=text], input[type=number] {
            height: 38px;
            width: 45%;
        }

        input:focus:invalid:focus {
            box-shadow: 0 0 0 rgba(255, 255, 255, 0.15);
            -webkit-box-shadow: 0 0 0 rgba(255, 255, 255, 0.15);
            -moz-box-shadow: 0 0 0 rgba(255, 255, 255, 0.15);
        }

        .layui-form {
            margin-left: 20%;
        }
    </style>
</head>
<body>

<%@ include file="base/top_menu.jsp" %>
<div class="container-fluid">
    <div class="row-fluid">
        <%@ include file="base/left_menu.jsp" %>
        <!--/span-->
        <div class="span10" id="content">
            <div class="row-fluid">
                <!-- block -->
                <div class="block">
                    <div class="navbar navbar-inner block-header">
                        <div class="muted pull-left">客户信息</div>
                    </div>
                    <div class="block-content collapse in">
                        <form class="layui-form">
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    客户名称：
                                </label>
                                <div class="layui-input-block">
                                    <input type="text" name="cusName" required lay-verify="required"
                                           placeholder="请输入客户名称" autocomplete="off"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    联系电话：
                                </label>
                                <div class="layui-input-block">
                                    <input type="text" name="cusPhone" placeholder="请输入联系方式" autocomplete="off"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    联系地址：
                                </label>
                                <div class="layui-input-block">
                                    <input type="text" name="cusAddr" placeholder="请输入联系地址" autocomplete="off"
                                           class="layui-input">
                                </div>
                            </div>

                            <div class="layui-input-block">
                                <input type="button" lay-submit lay-filter="formSubmit"
                                       class="layui-btn layui-btn-normal" value="立即提交"/>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <hr>
</div>
</body>
<script>
    $(function () {
        // 导航设置
        var remove = ['top_index', 'left_index'];
        var add = ['customer', 'customer_list'];
        initMenu(remove, add);

        var cus_name = '';
        var cus_phone = '';
        var cus_addr = '';
        var id = '';
        if (getUrlParam("type") && getUrlParam("id")) {
            id = getUrlParam("id");
            $.get('${rootPath}/customer_getCus?id=' + id, function (data) {
                cus_name = data.cusName;
                cus_phone = data.cusPhone;
                cus_addr = data.cusAddr;
                $("input[name=cusName]").val(cus_name);
                $("input[name=cusPhone]").val(cus_phone);
                $("input[name=cusAddr]").val(cus_addr);
            });
        }


        $("input[name=cusPhone]").blur(function () {
            var phone = $(this).val();
            if (phone && !validPhone(phone)) {
                $(this).val("");
                layer.msg('手机号码不正确', {icon: 5, shift: 6});
            }
        });

        $("input[type=button]").click(function () {
            var errNum = 0;
            var cusName = $("input[name=cusName]").val().trim();
            var phone = $("input[name=cusPhone]").val();

            if (phone && !validPhone(phone)) {
                $("input[name=cusPhone]").val("");
                layer.msg('手机号码不正确', {icon: 5, shift: 6});
                errNum++;
            }
            var url = '${rootPath}/customer_save';
            var data = $(".layui-form").serialize();
            if (id) {
                if (cusName == cus_name && phone == cus_phone && cus_addr == $("input[name=cusAddr]").val()) {
                    layer.msg("您暂未做修改！");
                    errNum++;
                }
                url = '${rootPath}/customer_edit';
                data += "&id=" + id;
            }
            if (errNum < 1) {
                $.ajax({
                    url: url,
                    dataType: "json",
                    type: "post",
                    data: data,
                    success: function (data) {
                        if (data.status == 1) {
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 1000,
                                end: function () {
                                    location.href = "http://" + window.location.host + '${rootPath}/page_customer';
                                }
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            }


        });

        $(document).keydown(function (event) {
            if (event.keyCode == 13) {
                $("input[type=button]").trigger('click');
            }
        });

    });

</script>
</html>
